package com.tyk.bookstore.front.member.service.impl;


import com.tyk.bookstore.front.common.core.util.AssertUtil;
import com.tyk.bookstore.front.common.dal.mapper.dto.SinDto;
import com.tyk.bookstore.front.member.model.dto.Address4OrderDto;
import com.tyk.bookstore.front.member.model.entity.DeliveryAddressDo;
import com.tyk.bookstore.front.member.model.result.DeliveryAddressResultEnum;
import com.tyk.bookstore.front.member.repository.DeliveryAddressRepository;
import com.tyk.bookstore.front.member.service.DeliveryAddressApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

/**
 * @author tyk
 * @description 收货地址对内暴露接口
 */
@Service
@RequiredArgsConstructor
public class DeliveryAddressApiServiceImpl implements DeliveryAddressApiService {
    private final DeliveryAddressRepository deliveryAddressRepository;

    @Override
    public String getConsigneeById(Long id) {
        SinDto<String> dto = deliveryAddressRepository.selectSin1Eq(
                DeliveryAddressDo::getConsignee,
                DeliveryAddressDo::getId, id,
                DeliveryAddressDo.class
        );
        AssertUtil.notNull(dto, DeliveryAddressResultEnum.ADDRESS_NOT_FOUND);
        return dto.getV1();
    }

    @Override
    public Address4OrderDto getAddress4OrderDtoById(Long id, Long memberId) {
        Address4OrderDto dto = deliveryAddressRepository.selectAddress4OrderDtoById(id, memberId);
        AssertUtil.notNull(dto, DeliveryAddressResultEnum.ADDRESS_NOT_FOUND);
        return dto;
    }

    @Override
    public Long geAreaCodeById(Long id) {
        SinDto<BigInteger> dto = deliveryAddressRepository.selectSin1Eq(
                DeliveryAddressDo::getAreaCode,
                DeliveryAddressDo::getId, id,
                DeliveryAddressDo.class
        );
        AssertUtil.notNull(dto, DeliveryAddressResultEnum.ADDRESS_NOT_FOUND);
        return dto.getV1().longValue();
    }

    @Override
    public void assertExistById(Long id, Long memberId) {
        AssertUtil.isTrue(
                deliveryAddressRepository.exist2Eq(
                        DeliveryAddressDo::getId, id,
                        DeliveryAddressDo::getMemberId, memberId,
                        DeliveryAddressDo.class
                ),
                DeliveryAddressResultEnum.ADDRESS_NOT_FOUND
        );
    }
}
